package com.example.demo.mvvm.ui.vm.fragment;

import android.content.Intent;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableField;
import android.databinding.ObservableList;
import android.text.TextUtils;
import android.widget.EditText;

import com.example.demo.mvvm.BR;
import com.example.demo.mvvm.R;
import com.example.demo.mvvm.bean.ArticleBean;
import com.example.demo.mvvm.bean.HotKeyBean;
import com.example.demo.mvvm.bean.ListDto;
import com.example.demo.mvvm.common.DividerLine;
import com.example.demo.mvvm.net.BaseObserver;
import com.example.demo.mvvm.net.NetRequest;
import com.example.demo.mvvm.ui.activity.ChildrenActivity;
import com.example.demo.mvvm.ui.activity.WebActivity;
import com.example.demo.mvvm.utils.ActivityUtils;
import com.example.demo.mvvm.utils.OnItemClickListener;
import com.google.android.flexbox.AlignItems;
import com.google.android.flexbox.FlexDirection;
import com.google.android.flexbox.FlexWrap;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.google.android.flexbox.JustifyContent;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * Created by 80711 on 2018/5/21.
 */

public class SearchFragmentVm {

    public int pageNum = 0 ;

    public EditText editText;

    public int DIVIDER_ITEM = DividerLine.HORIZONTAL;

    public final FlexboxLayoutManager flexboxLayoutManager = new FlexboxLayoutManager(ActivityUtils.peek()){{
        this.setFlexDirection(FlexDirection.ROW);
        this.setFlexWrap(FlexWrap.WRAP);
        this.setJustifyContent(JustifyContent.SPACE_AROUND);
        this.setAlignItems(AlignItems.BASELINE);
    }};

    public final ObservableList<HotKeyBean> mHotKeyBeans = new ObservableArrayList<>(); // 热词搜索

    public final ObservableList<ArticleBean> mArticleBeans = new ObservableArrayList<>();//搜索的文章

    public ObservableField<OnRefreshLoadmoreListener> listener = new ObservableField<>();

    public final ItemBinding<HotKeyBean> hotKeyBeanItemBinding = ItemBinding.<HotKeyBean>of(BR.item , R.layout.item_hot_key).bindExtra(BR.listener, new OnItemClickListener<HotKeyBean>() {
        @Override
        public void onItemClick(int viewId, HotKeyBean data) {
            editText.setText(data.getName());
            pageNum = 0;
            searchDatas(null);
        }
    });

    public final ItemBinding<ArticleBean> articleBeanItemBinding = ItemBinding.<ArticleBean>of(BR.item, R.layout.item_home_article).bindExtra(BR.listener, new OnItemClickListener<ArticleBean>() {
        @Override
        public void onItemClick(int viewId , ArticleBean data) {
            switch (viewId){
                case R.id.root:
                    ActivityUtils.push(WebActivity.class , new Intent().putExtra("activityUrl" ,data.getLink() ));
                    break;
                case R.id.chapter_tv:
                    ActivityUtils.push(ActivityUtils.peek() , ChildrenActivity.class , new Intent().putExtra("articleBean" , data));
                    break;
            }
        }
    });


    public SearchFragmentVm(EditText editText){
        this.editText = editText;
        listener.set(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                searchDatas(refreshlayout);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getHotKeyDatas(refreshlayout);
                searchDatas(refreshlayout);
            }
        });

    }

    public  void getHotKeyDatas(RefreshLayout layout){
        NetRequest.getHotKeyDatas(new BaseObserver<List<HotKeyBean>>(ActivityUtils.peek() , layout , true) {
            @Override
            public void onSuccess(List<HotKeyBean> hotKeyBeans, RefreshLayout refreshLayout) {
                mHotKeyBeans.clear();
                mHotKeyBeans.addAll(hotKeyBeans);
            }

            @Override
            public void onFailure(int code, String message, RefreshLayout refreshLayout) {

            }
        });
    }

    public void searchDatas( RefreshLayout layout){
        if(TextUtils.isEmpty(editText.getText().toString())){
            //Toast.makeText(ActivityUtils.peek(), "请输入关键字进行搜索", Toast.LENGTH_SHORT).show();
            return;
        }
        NetRequest.getSearchDatas(pageNum, editText.getText().toString() , new BaseObserver<ListDto<ArticleBean>>(ActivityUtils.peek() , layout , true) {
            @Override
            public void onSuccess(ListDto<ArticleBean> articleBeanListDto, RefreshLayout refreshLayout) {
                if(pageNum == 0){
                    mArticleBeans.clear();
                }
                if(articleBeanListDto.getDatas() != null && articleBeanListDto.getDatas().size() != 0){
                    pageNum +=1;
                }
                mArticleBeans.addAll(articleBeanListDto.getDatas());
            }

            @Override
            public void onFailure(int code, String message, RefreshLayout refreshLayout) {

            }
        });
    }


}
